const {
  getEventData
} = getApp()
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
    title: {
      type: String,
      value: ''
    },
    value: {
      type: null,
      value: ''
    },

    required: {
      type: Boolean,
      value: false
    },
 
    nodeKey: {
      type: String,
      value: 'name'
    },
    valueKey: {
      type: String,
      value: 'text'
    },
    options: Array,
    disabled: Boolean,
  },
  methods: {
  
    onClick(event) {

      if (this.data.disabled) return;
      const { item } = getEventData(event);
      if (!item) return;
      const value = this.data.value;
      if (!value) {
        value = []
      }
      const index = value.indexOf(item[this.data.nodeKey]);
      if (index > -1) {
        value.splice(index, 1)
      } else {
        value.push(item[this.data.nodeKey])
      }

      this.setData({
        value
      })

      this.bindChange();
    },

    bindChange() {
      this.triggerEvent('change', this.data.value);
    }
  }
})